package com.scalar.forecast.systemprefs;

import com.scalar.forecast.systemprefs.dao.SystemPrefsDAO;
import com.scalar.forecast.forecastSetup.ForecastTimeFrame;
import com.scalar.forecast.forecastSetup.dao.ForecastSetupDAO;
import com.scalar.ScalarException;

/**
 * Singleton class that returns the System Preferences property values.
 *
 * @author Basha
 * @version 1.0
 */

public class SystemPreferencesSingleton
{
    private static SystemPreferences pSystemPreferences = null;
    private static ForecastTimeFrame pForecastTimeFrame = null;

    private SystemPreferencesSingleton()
    {

    }

    private static SystemPreferences getSystemPreferences()
    {
        if( pSystemPreferences == null )
        {
            try
            {
                pSystemPreferences = SystemPrefsDAO.loadSystemPreferences();
            }
            catch( Exception e )
            {
                System.out.println( "Error while loading the system preferences" );
                e.printStackTrace();
            }

        }
        return pSystemPreferences;
    }

    private static ForecastTimeFrame getForecastTimeFrame()
    {
        if ( pForecastTimeFrame == null )
        {
            try
            {
                pForecastTimeFrame = ForecastSetupDAO.getForecastTimeFrame( null );
            }
            catch( ScalarException e )
            {
                System.out.println ( "Error while loading the forecast time frame in System Preferences" );
                e.printStackTrace();
            }
        }
        return pForecastTimeFrame;
    }
    /**
     * Used to indicate to the singlton class to update the system preferences from the database
     *
     */
    public static void update()
    {
        pSystemPreferences = null;
        pForecastTimeFrame = null;
    }


    public static String getCanBuildMasterCanOverrideSFC()
    {
        if( getSystemPreferences() != null )
        {
            return getSystemPreferences().getCanBuildMasterCanOverrideSFC();
        }
        else
        {
            return "";
        }
    }

    public static String getPartNumberType()
    {
        if( getSystemPreferences() != null )
        {

            return getSystemPreferences().getPartNumberType();
        }
        else
        {
            return "";
        }
    }

    public static String getPriceType()
    {
        if( getSystemPreferences() != null )
        {

            return getSystemPreferences().getPriceType();
        }
        else
        {
            return "";
        }
    }

    /**
     * returns the time frame freezing point i.e no of days
     * @return
     */
    public static int getFreezingPoint ()
    {
        int freezingPoint = 0;
        if ( getForecastTimeFrame() != null )
        {
            try
            {
                freezingPoint = Integer.parseInt ( getForecastTimeFrame().getTfCompany() );
            }catch ( Exception ignore ){}
        }

        return freezingPoint;
    }

    /**
     *
     * @return the forecast cycle value i.e weekly, monthly or quarterly.
     */
    public static String getForecastCycle() {
        if(getForecastTimeFrame() != null ) {
            return getForecastTimeFrame().getTfCycle();
        }
        return null;
    }
}
